﻿var Dialog = function(target,obj){
    if(!target) return;
    var content;
    var url;
    if(typeOf(target) === 'element') {
        content = target.clone(true,true);
    }
    
    else if(typeOf(target) === 'string') {
        url = target;
    }
    var winSize = window.getSize();
    var bodySize = window.getScrollSize();
    var self = this;
    this.options = Object.merge({
        url : url,
        content: content,
        evalScripts:false,
        method : 'post', //请求方式
        data:'',         //数据
        width:540,       //dialog 宽度
        height:'auto',   //dialog 高度,默认为内容高度
        title:'&nbsp'    //dialog title 默认为空
    }, obj || {});
    this.dialogEle = new Element('div',{
        'class':'dialog'
    }).store('instance', this);
    this.contentEle = new Element('div',{
        'class':'dialog-content',
        'html':'<div class="loading"><img src="images/loading.gif" alt="loading" /></div>'
    });
    if(this.options.type != 'nohead') {
        this.headEle = new Element('div',{
            'class':'dialog-head'
        });
    }
    this.closeEle = new Element('span',{
        'class':'close',
        'title':'关闭'
    });
    var init = function(){
        // if(obj){
        //     self.options = Object.merge(self.options,obj);
        // }

        if(url) {
            if(self.options.async="iframe") iframeLoad(self.options.url);
            else ajaxLoad(self.options.url);
        }
        else if(content){
            contentLoad(self.options.content);
            if(self.options.evalScripts){
                self.options.script = content.getElement('script').innerHTML;
            }
        };
        applyElement();
    }
    this.close = function(){
        self.dialogEle.destroy();
        self.options.onClose && self.options.onClose();
        self.mask.setStyle('display','none');
    };
    var applyElement = function(){
        if(self.options.modal) {
            if($('dialog_mask')){
                self.mask = $('dialog_mask');
            }else{
                self.mask = new Element('div',{
                    'id':'dialog_mask'
                });
                self.mask.inject(document.body);
            }
            var maskHeight = Math.max(winSize.y,bodySize.y);
            self.mask.setStyles({
                'position':'absolute',
                'display':'block',
                'top':0,
                'left':0,
                'width':winSize.x,
                'height':maskHeight,
                'z-index':65534,
                'background':'#000000',
                'opacity':0.5
            });
        }
        self.closeEle.addEvent('click',function(){
            self.close();
        });
        if(self.headEle) {
            self.headEle.set('html',self.options.title).inject(self.dialogEle);
            self.closeEle.inject(self.headEle);
        }
        self.contentEle.inject(self.dialogEle);
        self.dialogEle.inject(document.body);
        self.style();
    };
    this.style = function(){
        var left = (winSize.x - self.options.width)/2;
        var _top = (winSize.y - self.options.height)/2 + $(document.body).getScroll().y;
        if(self.options.height != 'auto'){
            self.contentEle.setStyles({
                'height':self.options.height,
                'overflow-y':'auto'
            });
        }
        self.dialogEle.setStyles({
            'width':self.options.width,
            'position':'absolute',
            'left':left,
            'top':_top,
            'z-index':65535,
            'opacity':0
        });
        self.dialogEle.fade(1);
        if(self.options.evalScripts){
            new Element('script',{'html':self.options.script}).inject($$('head')[0]);
        }           
    }
    var contentLoad = function(content) {
        if(content.get('tag')=='img') {
            self.contentEle.innerHTML = new Element('div').adopt(content).innerHTML;
        }
        else self.contentEle.innerHTML = content.innerHTML;

    }
    var ajaxLoad = function(url){
        if($(url)){
            self.contentEle.innerHTML = $(url).innerHTML;
            self.style();
        }else{
            new Request({
                'url':url,
                'evalScripts':true,
                'method' : self.options.method,
                'onComplete':function(re){
                    self.contentEle.set('html',re);
                    self.style();
                }
            }).send(self.options.data);
        }
    }
    var iframeLoad = function(url) {
        self.contentEle.innerHTML = '<iframe allowtransparency="allowtransparency" align="middle" frameborder="0" height="100%" width="100%" scrolling="auto" src="'+url+'">请使用支持iframe框架的浏览器。</iframe>';
    }
    init();
}
